package com.yimi.yinhepda.views.login.activity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.pda.ym.utils.SysInfo;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.base.BaseActivity2;
import com.yimi.yinhepda.utils.Common;
import com.yimi.yinhepda.views.login.model.CompanyFindAllInfo;
import com.yimi.yinhepda.views.login.model.respones.CompanyFindAllResponse;
import com.pda.ym.utils.Constants;
import com.pda.uce.commom.util.MyDialog;
import com.pda.ym.utils.SharedPreferUtil;
import com.pda.ym.utils.SystemEnvironment;
import com.pda.ym.utils.SpinnerData;
import com.yimi.yinhepda.utils.spinner.SpinnerWithCodeAdapter;
import com.yimi.yinhepda.views.login.contract.QueryCompanyContract;
import com.yimi.yinhepda.views.login.presenter.QueryCompanyPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * 登陆时，公司查询
 *
 * @author 000432
 */
public class QueryCompanyDialogActivity
        extends BaseActivity2<QueryCompanyContract.QueryCompanyView, QueryCompanyContract.QueryCompanyBiz, QueryCompanyPresenter>
        implements QueryCompanyContract.QueryCompanyView, AdapterView.OnItemSelectedListener {
    /**
     * 服务器选择器
     */
    @Bind(R.id.serverIPSpinner)
    Spinner serverSpinner;
    /**
     * 公司名称选择器
     */
    @Bind(R.id.companyNameSpinner)
    Spinner companyNameSpinner;
    private List<SpinnerData> companyNameSpinnerList;
    private List<SpinnerData> serverNameSpinnerList;
    private SpinnerWithCodeAdapter compNameAdapter;
    private boolean isSelect = false;
    private boolean isFirst = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.companycode);
        ButterKnife.bind(this);
        initView();
        //获取公司信息
        mT.getCompanyInfo();
    }

    @Override
    public String getPageName() {
        return "公司查询界面";
    }

    @Override
    public QueryCompanyPresenter createPresenter() {
        return new QueryCompanyPresenter();
    }

    public void initView() {
        //校验设备型号
        String phoneModel = SysInfo.getInstance().getPhoneModel();
        if (phoneModel != null && phoneModel.equals(Common.DEV_MODEL)) {
            //横屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        }

        // 公司名称列表
        companyNameSpinnerList = new ArrayList<>();
        // 初始化服务器列表
        serverNameSpinnerList = new ArrayList<>();
        serverNameSpinnerList = SystemEnvironment.getStatusListForSpinner(false);
        SpinnerWithCodeAdapter serverAdapter = new SpinnerWithCodeAdapter(this, serverNameSpinnerList, serverSpinner);
        serverAdapter.setSelectedOption(Constants.getServerIp());
        serverSpinner.setOnItemSelectedListener(this);
    }

    @OnClick({R.id.btn_save, R.id.btn_cancel})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                saveData();
                break;
            case R.id.btn_cancel:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 保存服务器数据、公司编码和公司名称
     */
    @Override
    public void saveData() {
        mT.saveData(serverNameSpinnerList, serverSpinner, companyNameSpinnerList, companyNameSpinner);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (isFirst) {
            //禁止spinner首次自动调用该方法
            isFirst = false;
            return;
        }
        //选择服务器后，重新初始化公司名称
        SpinnerData serverInfo = serverNameSpinnerList.get(position);
        String serverIp = serverInfo.getValue();
        String serverName = serverInfo.getText();
        SharedPreferUtil.getInstance().setServerIP(serverIp);
        SharedPreferUtil.getInstance().setServerName(serverName);
        //重新设置服务器变量
        Constants.setNewLoad();
        isSelect = true;
        //获取公司信息
        mT.getCompanyInfo();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void getCompanyInfoSuccess(Object obj) {
        dismissPgDlg();
        if (obj instanceof CompanyFindAllResponse) {
            companyNameSpinnerList.clear();
            companyNameSpinnerList.add(new SpinnerData(getString(R.string.please_select_companyName), ""));
            CompanyFindAllResponse companyResponse = (CompanyFindAllResponse) obj;
            List<CompanyFindAllInfo> resultList = companyResponse.getData();
            if (resultList != null && !resultList.isEmpty()) {
                for (CompanyFindAllInfo companyEntity : resultList) {
                    SpinnerData companyInfo = new SpinnerData();
                    companyInfo.setText(companyEntity.getShortName());
                    companyInfo.setValue(companyEntity.getCompCode());
                    companyNameSpinnerList.add(companyInfo);
                }
                compNameAdapter = new SpinnerWithCodeAdapter(this, companyNameSpinnerList, companyNameSpinner);
                if (isSelect) {
                    isSelect = false;
                    compNameAdapter.setSelectedOptionForCompany("");
                } else {
                    compNameAdapter.setSelectedOptionForCompany(SharedPreferUtil.getInstance().getCompanyName());
                }
            } else {
                MyDialog.showAlertDialog(this, "服务器没有数据");
            }
        }
    }

    @Override
    public void getCompanyInfoError(String msg) {
        compNameAdapter = new SpinnerWithCodeAdapter(this, companyNameSpinnerList, companyNameSpinner);
        compNameAdapter.setSelectedOptionForCompany("");
        MyDialog.showAlertDialog(this, msg);
        dismissPgDlg();
    }
}
